package com.talent.study.disruptor.height;

import com.lmax.disruptor.BusySpinWaitStrategy;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author: hszhou
 * @Date: 2025/4/19 11:44
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
@Slf4j
public class Main {

    public static void main(String[] args) throws InterruptedException {


        ExecutorService es = Executors.newFixedThreadPool(4);
        ExecutorService es2 = Executors.newFixedThreadPool(4);

        //单生产者
        Disruptor<Trade> disruptor = new Disruptor<>(Trade::new, 16,es2, ProducerType.SINGLE,new BusySpinWaitStrategy());



        log.info("构建");



        //串行操作
//        disruptor.handleEventsWith(new Handler1())
//                .handleEventsWith(new Handler2())
//                .handleEventsWith(new Handler3())
//                .handleEventsWith(new Handler4());


        //并行操作 2种方式
//  第一种
//        disruptor.handleEventsWith(new Handler1());
//        disruptor.handleEventsWith(new Handler2());
//        disruptor.handleEventsWith(new Handler3());
//        disruptor.handleEventsWith(new Handler4());
        // 第二种
     //   disruptor.handleEventsWith(new Handler1(),new Handler2(),new Handler3(),new Handler4() );

        //菱形操作

        /*
         *    c1
         * c--   -c3
         *    c2
         */

        //disruptor.handleEventsWith(new Handler1(),new Handler2()).handleEventsWith(new Handler3());

//        EventHandlerGroup<Trade> tradeEventHandlerGroup = disruptor.handleEventsWith(new Handler1(), new Handler2());
//        tradeEventHandlerGroup.then(new Handler3());


        /**
         *    c1   c2
         * c            c3
         *    c4   c5
         */

        //六变形
//        Handler1 handler1 = new Handler1();
//        Handler2 handler2 = new Handler2();
//        Handler3 handler3 = new Handler3();
//        Handler4 handler4 = new Handler4();
//        Handler5 handler5 = new Handler5();
//        disruptor.handleEventsWith(handler1,handler4);
//        disruptor.after(handler1).handleEventsWith(handler2);
//        disruptor.after(handler4).handleEventsWith(handler5);
//        disruptor.after(handler1,handler4).handleEventsWith(handler3);

        RingBuffer<Trade> ringBuffer = disruptor.start();

        log.info("启动");
        CountDownLatch downLatch = new CountDownLatch(1);
        es.submit(new TradePublish(downLatch,disruptor));

        downLatch.await();
        disruptor.shutdown();
        es.shutdown();
    }
}
